﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class CmsCatchParseUrl
    {
        private readonly KeRui.DAL.CmsCatchParseUrl dal = new KeRui.DAL.CmsCatchParseUrl();

        public int Add(KeRui.Model.CmsCatchParseUrl model)
        {
            return this.dal.Add(model);
        }

        public List<KeRui.Model.CmsCatchParseUrl> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.CmsCatchParseUrl> list = new List<KeRui.Model.CmsCatchParseUrl>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.CmsCatchParseUrl item = new KeRui.Model.CmsCatchParseUrl();
                    if (dt.Rows[i]["ParseUrlID"].ToString() != "")
                    {
                        item.ParseUrlID = int.Parse(dt.Rows[i]["ParseUrlID"].ToString());
                    }
                    item.ParseUrlDomain = dt.Rows[i]["ParseUrlDomain"].ToString();
                    item.ParseUrlRegex = dt.Rows[i]["ParseUrlRegex"].ToString();
                    if (dt.Rows[i]["ParseContentID"].ToString() != "")
                    {
                        item.ParseContentID = int.Parse(dt.Rows[i]["ParseContentID"].ToString());
                    }
                    item.ParseContentName = dt.Rows[i]["ParseContentName"].ToString();
                    if (dt.Rows[i]["UseCount"].ToString() != "")
                    {
                        item.UseCount = int.Parse(dt.Rows[i]["UseCount"].ToString());
                    }
                    if (dt.Rows[i]["UseLastTime"].ToString() != "")
                    {
                        item.UseLastTime = DateTime.Parse(dt.Rows[i]["UseLastTime"].ToString());
                    }
                    if (dt.Rows[i]["State"].ToString() != "")
                    {
                        item.State = int.Parse(dt.Rows[i]["State"].ToString());
                    }
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    if (dt.Rows[i]["CreateUserID"].ToString() != "")
                    {
                        item.CreateUserID = int.Parse(dt.Rows[i]["CreateUserID"].ToString());
                    }
                    item.CreateUserName = dt.Rows[i]["CreateUserName"].ToString();
                    if (dt.Rows[i]["UpdateTime"].ToString() != "")
                    {
                        item.UpdateTime = DateTime.Parse(dt.Rows[i]["UpdateTime"].ToString());
                    }
                    if (dt.Rows[i]["UpdateUserID"].ToString() != "")
                    {
                        item.UpdateUserID = int.Parse(dt.Rows[i]["UpdateUserID"].ToString());
                    }
                    item.UpdateUserName = dt.Rows[i]["UpdateUserName"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }

        public void Delete(int ParseUrlID)
        {
            this.dal.Delete(ParseUrlID);
        }

        public bool Exists(int ParseUrlID)
        {
            return this.dal.Exists(ParseUrlID);
        }

        public List<KeRui.Model.CmsCatchParseUrl> GetList(string strCondition)
        {
            string strWhere = "";
            string strOrder = "";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("CmsCatchParseUrl", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsCatchParseUrl> GetList(string where, string order, int size, int index)
        {
            string strWhere = where;
            string strOrder = order;
            int pageSize = size;
            int pageIndex = index;
            DataSet set = new KeRui.DAL.Common().GetList("CmsCatchParseUrl", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsCatchParseUrl> GetListByCache(string strCondition)
        {
            string cacheKey = "CmsCatchParseUrlList-" + strCondition;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(strCondition);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.CmsCatchParseUrl>) cache;
        }

        public KeRui.Model.CmsCatchParseUrl GetModel(int ParseUrlID)
        {
            return this.dal.GetModel(ParseUrlID);
        }

        public KeRui.Model.CmsCatchParseUrl GetModelByCache(int ParseUrlID)
        {
            string cacheKey = "CmsCatchParseUrlModel-" + ParseUrlID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(ParseUrlID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.CmsCatchParseUrl) cache;
        }

        public void Update(KeRui.Model.CmsCatchParseUrl model)
        {
            this.dal.Update(model);
        }
    }
}

